import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import store from '../store'
Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      isPublic: true
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
    children: [
      {
        path: 'category',
        name: 'Category',
        component: () => import(/* webpackChunkName: "category" */ '../views/Category.vue')
      },
      {
        path: 'video',
        name: 'Video',
        component: () => import(/* webpackChunkName: "video" */ '../views/Video.vue')
      },
      {
        path: 'article',
        name: 'Article',
        component: () => import(/* webpackChunkName: "article" */ '../views/Article.vue')
      },
      {
        path: 'advert',
        name: 'Advert',
        component: () => import(/* webpackChunkName: "advert" */ '../views/Advert.vue')
      },
      {
        path: 'account',
        name: 'Account',
        component: () => import(/* webpackChunkName: "account" */ '../views/Account.vue')
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  if (!to.meta.isPublic && !store.state.token) return next({path: '/login'});
  next();
})

export default router
